package com.accessibleworlds.report;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.accessibleworlds.R;
import com.accessibleworlds.util.SharedVariables;
	
/**
 * Lets users see images attached to reports in a sequential manner with swipe gestures.
 */
public class ReportImageSwitcher extends Activity implements ViewSwitcher.ViewFactory{
	
	private ImageSwitcher imageSwitcher;
	private Gallery gallery;
	
	private String [] imageIDList;
	private Drawable [] images;
	
	private Bundle bundle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.reportimageswitcher);
		
		initializeVariables();
    }
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ReportImageSwitcher.this,ReportView.class);
		Bundle bundleSend = new Bundle();
		bundleSend.putString("previousID", bundle.getString("previousID"));
		System.out.println("p");
		bundleSend.putString("reportID", ""+bundle.getString("reportID"));
		System.out.println("a");
		bundleSend.putString("status", bundle.getString("status"));
		System.out.println("t");
		bundleSend.putString("type", ""+bundle.getString("type"));
		System.out.println("l");
		bundleSend.putString("title", bundle.getString("title"));
		System.out.println("a");
		bundleSend.putString("description", bundle.getString("description"));
		System.out.println("d");
		bundleSend.putString("latitude", ""+bundle.getString("latitude"));
		System.out.println("i");
		bundleSend.putString("longitude", ""+bundle.getString("longitude"));
		System.out.println("!");
		bundleSend.putString("rate", bundle.getString("rate"));
		intent.putExtras(bundleSend);
		startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);
		finish();
	}
	
	/**
	 * Initializes view elements.
	 */
	private void initializeVariables() {		
		bundle = getIntent().getExtras();
		imageIDList = bundle.getStringArray("imageIDList");
		
		images = new Drawable[imageIDList.length];
		
		for(int i=0; i<imageIDList.length;i++) 
			images[i] = Drawable.createFromPath(SharedVariables.folderName+File.separator+imageIDList[i]+".jpg");
		
		imageSwitcher = (ImageSwitcher)findViewById(R.id.reportImageSwitcherSwitcher);
		gallery = (Gallery)findViewById(R.id.reportImageSwitcherGallery);
		
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
		
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				imageSwitcher.setImageDrawable(images[position]);
			}

			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	}

	/**
	 * Creates a new ImageView with necessary 
	 */
	public View makeView() {
		ImageView imageView = new ImageView(this);
		imageView.setBackgroundColor(0xFF000000);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        return imageView;
	}
	
	public class ImageAdapter extends BaseAdapter {
		
		private Context context;
		int defaultItemBackground;
		
		public ImageAdapter(Context context) {
			this.context = context;
			TypedArray styleAttributes = context.obtainStyledAttributes(R.styleable.gallery);
			defaultItemBackground = styleAttributes.getResourceId(R.styleable.gallery_android_galleryItemBackground, 0);
			styleAttributes.recycle();
        }

		public int getCount() {
			return imageIDList.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
            return position;
        }

		public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setAdjustViewBounds(true);
            imageView.setImageDrawable(images[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setBackgroundResource(defaultItemBackground);
            return imageView;
		}
	}
}